package cn.itcast.bos.shiro;

import javax.annotation.Resource;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import cn.itcast.bos.dao.base.IUserDao;
import cn.itcast.bos.domain.User;

public class BosRealm extends AuthorizingRealm{
	@Resource
	private IUserDao userDao; 
	
	@Override  //认证
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		UsernamePasswordToken mytoken =(UsernamePasswordToken) token;
		String username = mytoken.getUsername();
		char[] password = mytoken.getPassword();
		User user = userDao.findByUsername(username);
		if (user == null) {
			return null;
		}
		
		//new SimpleAuthenticationInfo(principal, credentials, realmName)
		SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), this.getClass().getName());
		return info;
	}
	
	@Override // 授权
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		info.addStringPermission("staff");
		return info;
	}

	

}
